﻿var input = document.getElementById('sendText');
var recv = document.getElementById('recvText');

var wsImpl = window.WebSocket || window.MozWebSocket;
recv.value += "connecting to server\r\n";

// create a new websocket and connect
window.ws = new wsImpl('ws://127.0.0.1:2000/');

// when data is comming from the server, this metod is called
ws.onmessage = function (evt) {
    recv.value += evt.data + '\r\n';
};

// when the connection is established, this method is called
ws.onopen = function () {
    recv.value += 'connection open\r\n';
};

// when the connection is closed, this method is called
ws.onclose = function () {
    recv.value += 'connection closed\r\n';
}

// 发送消息
function SendText() {
    ws.send(input.value);
}